package com.ruoyi.psi.sale.delivery.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.produce.material.domain.ProduceMaterialRequisition;
import com.ruoyi.psi.sale.delivery.domain.*;
import com.ruoyi.psi.sale.delivery.service.ISaleDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售订单出库
 */
@RestController
@RequestMapping("/psi/sale/delivery")
@Api(value = "销售订单出库模块", tags = "销售订单出库接口")
public class SaleDeliveryController extends BaseController {

    @Autowired
    private ISaleDeliveryService saleDeliveryService;

    /**
     * 获取销售订单出库
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取销售订单出库列表",notes = "查询所有销售订单出库信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SaleDeliveryCond cond) {
        startPage();
        List<SaleDeliveryDto> saleDeliveryDtos = saleDeliveryService.selectSaleDeliveryList(cond);
        return getDataTable(saleDeliveryDtos);
    }

    /**
     * 根据编号获取详细信息
     */
    @GetMapping(value = "/info")
    @ApiOperation(value = "根据编号获取详细信息",notes = "根据编号获取详细信息")
    public AjaxResult getInfo(@RequestParam String saleDeliveryId)
    {
        return AjaxResult.success(saleDeliveryService.selectSaleDeliveryById(saleDeliveryId));
    }

    /**
     * 导出销售订单出库
     *
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody SaleDeliveryCond cond) {
        List<SaleDeliveryDto> saleDeliveryDtos = saleDeliveryService.selectSaleDeliveryList(cond);
        ExcelUtil<SaleDeliveryDto> util = new ExcelUtil<>(SaleDeliveryDto.class);
        return util.exportExcel(saleDeliveryDtos, "销售订单出库单数据");
    }

    /**
     * 新增销售出库时，获取销售订单内未出库商品信息

     * @return
     */
    @ApiOperation(value = "获取销售订et单内未出库商品信息",notes = "新增销售出库时，获取销售订单内未出库商品信息")
    @GetMapping("/getSaleOrderListForDelivery")
    public AjaxResult getSaleOrderListForDelivery(@RequestParam String saleOrderId) {
        List<SaleDeliveryListDto> saleDeliveryListDtos = saleDeliveryService.getSaleOrderListForDelivery(saleOrderId);
        return AjaxResult.success(saleDeliveryListDtos);
    }

    /**
     * 新增销售订单出库
     * @param saleDelivery 销售订单出库单据
     * @return
     */
    @ApiOperation(value = "新增销售订单出库", notes = "新增销售订单出库信息")
    @ApiImplicitParam(name = "saleDelivery", value = "新增销售订单", dataType = "SaleDeliveryDomain")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SaleDeliveryDomain saleDelivery) {
        try {
            saleDeliveryService.insertSaleDelivery(saleDelivery);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 销售出库
     * 用于app
     * @param saleDelivery
     * @return
     */
    @PostMapping("/saleDeliveryStorage")
    public AjaxResult saleDeliveryStorage(@RequestBody SaleDeliveryDomain saleDelivery) {
        return toAjax(saleDeliveryService.saleDeliveryStorage(saleDelivery));
    }

    /**
     *
     * 更新销售订单出库
     * @param saleDelivery 销售订单出库单据
     * @return
     */
    @ApiOperation(value = "修改销售订单出库", notes = "修改销售订单出库")
    @ApiImplicitParam(name = "saleDelivery", value = "修改销售订单出库", dataType = "SaleDeliveryDomain")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SaleDeliveryDomain saleDelivery) {
        try {
            saleDeliveryService.updateSaleDelivery(saleDelivery);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除销售订单出库
     * 仅修改删除标志位
     * @param saleDelivery 销售订单出库单据
     * @return
     */
    @ApiOperation(value = "删除销售订单出库", notes = "删除销售订单出库,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody SaleDeliveryDomain saleDelivery) {

        return toAjax(saleDeliveryService.updateSaleDeliveryDeleteFlag(saleDelivery));
    }
}
